<template>
  <div class="grid-layout">
    <div v-if="title || $slots.title" class="grid-layout-title" :style="{ borderColor: borderColor }">
      <slot name="title">{{ title }}</slot>
    </div>
    <a-row type="flex" class="grid-layout-row">
      <a-col v-for="item of tableLayout" :key="item.name" :style="{ borderColor: borderColor }" v-bind="item.colSpan">
        <div class="grid-layout-col" :style="Object.assign({}, this.itemStyle, item.style)">
          <slot :name="item.name" />
        </div>
      </a-col>
    </a-row>
    <div v-if="bottom || $slots.bottom" class="grid-layout-bottom" :style="{ borderColor: borderColor }">
      <slot name="bottom">{{ bottom }}</slot>
    </div>
  </div>
</template>

<script>
export default {
  name: 'grid-layout',
  props: {
    title: { type: String, default: '' },
    bottom: { type: String, default: '' },
    layout: {
      type: Array,
      default: () => [
        { name: 'left', colSpan: 8 },
        { name: 'right', colSpan: 16 },
      ],
    },
    itemStyle: { type: Object, default: () => ({}) },
    borderColor: { type: String, default: '#e3e3e3' },
  },
  computed: {
    tableLayout() {
      return this.layout.map((item) => {
        const colSpan = typeof item.colSpan === 'number' ? { span: item.colSpan } : item;
        return Object.assign({}, item, { colSpan });
      });
    },
  },
};
</script>

<style scoped lang="less">
.grid-layout {
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  height: 100%;
  text-align: left;
  .grid-layout-title,
  .grid-layout-bottom {
    box-sizing: border-box;
    flex: none;
    padding: 10px 20px;
    border-bottom: 1px solid #e3e3e3;
    font-size: 26px;
    font-weight: 500;
    color: #fefffe;
  }
  & > .grid-layout-row {
    flex: auto;
    height: auto;
    min-height: 0;
    display: flex;
    flex-direction: row;
    flex-wrap: nowrap;
    overflow: hidden;
    & > .ant-col {
      box-sizing: border-box;
      height: 100%;
      &:not(:first-child) {
        border-left: 1px solid #e3e3e3;
      }
    }
    .grid-layout-col {
      display: flex;
      flex-direction: column;
      justify-content: flex-start;
      box-sizing: border-box;
      height: 100%;
      overflow: hidden auto;
    }
  }
}
</style>
